<?php
namespace app\admin\controller;
use app\admin\model\Customers;
use app\admin\model\Users;
use app\admin\controller\AdminAuth;
use think\Validate;
use think\Image;
use think\Db;
use think\Request;
use app\admin\collection\permissionCollection;
use app\admin\model\Group;

class AllcustomersController extends AdminAuth
{
	//模块基本信息
	private $data = array(
		'module_name' => '所有客戶',
		'module_url'  => '/admin/Allcustomers/',
		'module_slug' => 'Customers',
		'upload_path' => UPLOAD_PATH,
		'upload_url'  => '/public/uploads/',
    );
	public function index($group_id=0,$user_id=0){
        $where = [];
        input('mobile') ? $where['C.mobile'] = input('mobile') : false; //手机号
        input('c_nickname') ? $where['C.c_nickname'] = input('c_nickname') : false; //昵称
        input('customer_type') ? $where['C.customer_type'] = input('customer_type') : false; //客户类型

        if(input('startTime') && input('endTime')){
            $start = strtotime(input('startTime'));
            $end = strtotime(input('endTime'));
            $where['C.addtime'] = ['between',[$start, $end]];
        }

        //判断是否是超级管理员=======================================
        $is_admin = session('is_admin');
        $groups = $users = [];
        if($is_admin == 1){
            //获取所有部门
            $groups = Group::where('status',1)->field('group_name,id')->select();
            $users = permissionCollection::getUsersByGroupId($group_id);
        }else{
            $users = permissionCollection::getUsersByGroupId(session('user_group'));
            $user_array = array_column($users,'id');
            $where['D.user_id'] = ['in',$user_array];
        }
        input('user_select') ? $where['D.user_id'] = input('user_select') : false; //所属用户
        $this->assign('groups',$groups);
        $this->assign('users',$users);
        $this->assign('is_admin',$is_admin);
        //===========================================================

        $list = Db::name('distribution')
            ->alias('D')
            ->join('customers C','C.customers_id = D.customers_id')
            ->join('users U','U.id = D.user_id')
            ->field('C.*,U.nickname')
            ->where($where)
            ->order('C.customers_id', 'DESC')
            ->paginate(10);

        $this->assign('data',$this->data);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function read(Request $request)
    {
        $customers_id = input('customers_id');
        //默认值设置
        $item = Db::name('customers')->where(['customers_id'=>$customers_id])->find();
        $art_id = Db::name('distribution')->where(['customers_id'=>$customers_id])->value('user_id');  //查询当前客户的所属用户
        //判断是否是超级管理员=======================================
        $is_admin = session('is_admin');
        $users = [];
        if($is_admin != 1){
            $admin_users = permissionCollection::getUsersByGroupId(session('user_group'));
        }else{
            $admin_users = Db::name('users')->select();
        }
        $this->assign('admin_users',$admin_users);
        //===========================================================

        $this->assign('art_id',$art_id);
        $this->assign('item',$item);
        $this->assign('data',$this->data);
        return view();
    }
    public function update($customers_id)
    {
        $customers = new Customers();
        //$data = input('post.');
        $data['name'] = input('name');
        $data['c_nickname'] = input('c_nickname');
        $data['mobile'] = input('mobile');
        $data['sex'] = input('sex');
        $data['customer_levlel'] = input('customer_levlel');
        $data['customer_type'] = input('customer_type');
        $data['remark'] = input('remark');
        $data['addtime'] = time();
        $user_id = input('user_id');  //选择分配的用户id
        $rule = [
            //字段验证
            'name|用户名' => 'require',
            'mobile|手机号'   => 'length:11',
        ];
        // 数据验证
        $validate = new Validate($rule);
        $result   = $validate->check($data);
        if(!$result){
            return  $this->error($validate->getError());
        }
        if ($customers->validate(true)->where(['customers_id'=>$customers_id])->update($data)) {
            Db::name('distribution')->where(['customers_id'=>$customers_id])->update(['user_id'=>$user_id]); //修改客户的所属用户
            return $this->success('信息更改成功',$this->data['module_url'],'','1');
        } else {
            return $this->error($customers->getError());
        }
    }
    //所有客户跟进记录
    public function followloglist(){
        $customers_id = input('customers_id');
        $followloglist = Db::name('follow_up')->where(['customers_id'=>$customers_id])->order('follow_id desc')->paginate(10);
        $this->assign('followloglist',$followloglist);
        $this->assign('data',$this->data);
        return view();
    }
    /**
     * [delete 删除用户数据(伪删除)]
     */
    public function delete($customers_id)
    {
        // 真.删除，不想用伪删除，请用这段
        $customer = Customers::get($customers_id);
        $data['customers_id'] = $customers_id;
        if ($customer) {
            Db::name('distribution')->where(['customers_id'=>$customers_id])->delete();  //删除分配表
            $customer->delete();  //删除客户表
            $data['error'] = 0;
            $data['msg'] = '删除成功';
        } else {
            $data['error'] = 1;
            $data['msg'] = '删除失败';
        }
        return $data;
    }
    //删除跟进log
    public function delfollow(){
        $follow_id = input('follow_id');
        $data['follow_id'] = input('follow_id');
        if ($follow_id) {
            $delfollow = Db::name('follow_up')->where(['follow_id'=>$follow_id])->delete();
            $data['error'] = 0;
            $data['msg'] = '删除成功';
        } else {
            $data['error'] = 1;
            $data['msg'] = '删除失败';
        }
        return $data;
    }
    //跟进记录详情
    public function followdetail(){
        $follow_id = input('follow_id');
        $re_follow = Db::name('follow_up')->where(['follow_id'=>$follow_id])->find();
        $re_follow['follow_time'] = date('Y-m-d H:i',$re_follow['follow_time']);
        $re_follow['next_follow_time'] = date('Y-m-d H:i',$re_follow['next_follow_time']);
        return json($re_follow);
    }
    //更新跟進记录记录
    public function updatefollowlog(){
        $data = input('post.');
        $data['follow_time'] = strtotime($data['follow_time']);
        $data['next_follow_time'] = strtotime($data['next_follow_time']);
        $re = Db::name('follow_up')->where(['follow_id'=>$data['follow_id']])->update($data);
        $customers_id = Db::name('follow_up')->where(['follow_id'=>$data['follow_id']])->value('customers_id');
        if($re){
            return $this->success('更新成功',$this->data['module_url'].'followloglist/customers_id/'.$customers_id,'','1');
        }else{
            return $this->error('更新上失败',$this->data['module_url'].'followloglist/customers_id/'.$customers_id,'','1');
        }
    }

}